#include "stdafx.h"
//-------------------------------------------------------------------------------------------------
//ProcParamDef.cpp  - Parameter Definitions for Image Processing Steps       
//-------------------------------------------------------------------------------------------------

#include "TinyXmlLib.h"
#include "TinyXmlHelper.h"
#include "ImageExptLib.h"

#include "ImageProcStepLibAuto.h"
#include "ImageProcFn.h"
#include "ProcParamDef.h"

#include "ProcParamDefDouble.h"
#include "ProcParamDefString.h"
#include "ProcParamDefInt.h"

ProcParamDef *ProcParamDef::CreateProcParamDef(ProcParamDef *param)
{
	ProcParamDef *rc = '\0';

	if (param == '\0') 
		ImageExptLib::LogMsg("ProcParamDef::CreateProcParamDef", "ERROR: input param is null");	
	else
	{
		if (param->IsValid() == false)
			ImageExptLib::LogMsg("ProcParamDef::CreateProcParamDef", "ERROR: input param (%s) is invalid", param->GetName());	
		else
		{
			switch (param->GetTypeP())
			{
			case IPluginICProcStepsParamDef::doubleP:
				{
					ProcParamDefDouble *paramdef = dynamic_cast<ProcParamDefDouble *>(param);
					if (paramdef == '\0')
						ImageExptLib::LogMsg("ProcParamDef::CreateProcParamDef", "ERROR: input param (%s) is invalid for ProcParamDefDouble", param->GetName());
					else
						rc = new ProcParamDefDouble(paramdef->GetIndex(), paramdef->GetName(), paramdef->GetDescription(), paramdef->GetDirection(), paramdef->GetMinValue(), paramdef->GetMaxValue());
				}
				break;
				case IPluginICProcStepsParamDef::stringP:
				{
					ProcParamDefString *paramdef = dynamic_cast<ProcParamDefString *>(param);
					if (paramdef == '\0')
						ImageExptLib::LogMsg("ProcParamDef::CreateProcParamDef", "ERROR: input param (%s) is invalid for ProcParamDefString", param->GetName());
					else
						rc = new ProcParamDefString(paramdef->GetIndex(), paramdef->GetName(), paramdef->GetDescription(), paramdef->GetDirection(), paramdef->GetMinLength(), paramdef->GetMaxLength());
				}
				break;
				case IPluginICProcStepsParamDef::intP:
				{
					ProcParamDefInt *paramdef = dynamic_cast<ProcParamDefInt *>(param);
					if (paramdef == '\0')
						ImageExptLib::LogMsg("ProcParamDef::CreateProcParamDef", "ERROR: input param (%s) is invalid for ProcParamDefInt", param->GetName());
					else
						rc = new ProcParamDefInt(paramdef->GetIndex(), paramdef->GetName(), paramdef->GetDescription(), paramdef->GetDirection(), paramdef->GetMinValue(), paramdef->GetMaxValue());
				}
				break;
				default:
					ImageExptLib::LogMsg("ProcParamDef::CreateProcParamDef", "ERROR: input param is unknown type (%s)", param->GetTypeStr());	
				break;
			}
			if ((rc == '\0') || ( rc->Validate() == false))
				ImageExptLib::LogMsg("ProcParamDef::CreateProcParamDef", "ERROR: failed to create a valid param for %s", param->GetName());
		}
	}
	return rc;
}

ProcParamDef::ProcParamDef() :
	_refCnt(0),
	_index(IPluginICProcStepsParamDef::NotFound),
	_Name(IPluginICProcStepsParamDefBadValue),
	_Description(IPluginICProcStepsParamDefBadValue),
	_Dir(IPluginICProcStepsParamDef::input),
	_DefaultValue(IPluginICProcStepsParamDefBadDefaultValue),
	_Definition(IPluginICProcStepsParamDefInvalidDefinition),
	_error(true)
{

}

ProcParamDef::ProcParamDef(int index, const char *Name, const char *Description, IPluginICProcStepsParamDef::Direction dir) :
	_refCnt(0),
	_index(index),
	_Name((Name != '\0') ? Name : IPluginICProcStepsParamDefBadValue),
	_Description((Description != '\0') ? Description : IPluginICProcStepsParamDefBadValue),
	_Dir(dir),
	_DefaultValue(IPluginICProcStepsParamDefBadDefaultValue),
	_Definition(IPluginICProcStepsParamDefInvalidDefinition),
	_error(true)
{
	if ((_index < 0 ) || (_index > IPluginICProcStepsParamDef::MaxIndex))
		ImageExptLib::LogMsg("ProcParamDef::ProcParamDef(index,name,desc,dir)", "ERROR: index=%d, index < 0 or index > %d", _index, IPluginICProcStepsParamDef::MaxIndex);

	if (_Name == IPluginICProcStepsParamDefBadValue)
		ImageExptLib::LogMsg("ProcParamDef::ProcParamDef(index,name,desc,dir)", "ERROR: _Name=%s", IPluginICProcStepsParamDefBadValue );

	if (_Description == IPluginICProcStepsParamDefBadValue)
		ImageExptLib::LogMsg("ProcParamDef::ProcParamDef(index,name,desc,dir)", "ERROR: _Description=%s", IPluginICProcStepsParamDefBadValue );
}


ProcParamDef::~ProcParamDef(void)
{
	_error = true;
}

bool ProcParamDef::SetXml(void *xmlParam)
{
	bool rc = false;

	if ( IsValid() == false)
		ImageExptLib::LogMsg("ProcParamDef::SetXml", "ERROR: %s param %s is invalid", (GetDirection() == IPluginICProcStepsParamDef::input) ? "in" : "out", GetName());	
	else
	{
		TiXmlElement *param = static_cast<TiXmlElement *>(xmlParam);
		if (param == '\0')
			ImageExptLib::LogMsg("ProcParamDef::SetXml", "ERROR: param is null");	
		else
		{
			param->SetAttribute(IPluginICProcStepsParamAttribName, GetName());
			param->SetAttribute(IPluginICProcStepsParamAttribIndex, GetIndex());
			param->SetAttribute(IPluginICProcStepsParamAttribType, GetTypeStr());
			rc = true;
		}
	}
	return rc;
}

bool ProcParamDef::IsMatch (IPluginICProcStepsParam  *param, IPluginICProcStepsParamDef::Direction dir)
{
	bool rc = false;

	if ( IsValid() == false)
		ImageExptLib::LogMsg("ProcParamDef::SetXml", "ERROR: matching %s param %s is invalid", (GetDirection() == IPluginICProcStepsParamDef::input) ? "in" : "out", GetName());	
	else
	{
		if (param == '\0')
			ImageExptLib::LogMsg("ProcParamDef::IsMatch()", "ERROR: passed param is null (%s)", (dir == IPluginICProcStepsParamDef::input) ? "input" : "output");
		else
		{
			if ( param->IsValid() == false )
				ImageExptLib::LogMsg("ProcParamDef::IsMatch()", "ERROR: passed %s param %s is invalid", ((dir == IPluginICProcStepsParamDef::input) ? "input" : "output"), param->GetName());
			else
			{
				if (param->GetIndex() != _index)
					ImageExptLib::LogMsg("ProcParamDef::IsMatch()", "ERROR: passed %s param %s index %d does not match ProcParamDef::_index %d", ((dir == IPluginICProcStepsParamDef::input) ? "input" : "output"), param->GetName(), param->GetIndex(), _index);
				else
				{
					if (param->GetName() != _Name)
						ImageExptLib::LogMsg("ProcParamDef::IsMatch()", "ERROR: passed %s param %s does not match ProcParamDef::_Name %s", ((dir == IPluginICProcStepsParamDef::input) ? "input" : "output"), param->GetName(), _Name.c_str());				
					else
					{
						if (dir != _Dir)
							ImageExptLib::LogMsg("ProcParamDef::IsMatch()", "ERROR: passed param %s direction %s does not match ProcParamDef::_Dir %s", param->GetName(), ((dir == IPluginICProcStepsParamDef::input) ? "input" : "output"), ((_Dir == IPluginICProcStepsParamDef::input) ? "input" : "output"));
						else
						{
							rc = true;
						}
					}
				}
			}
		}
	}
	return rc;
}

bool ProcParamDef::IsValid()
{ 
	return (_error == true) ? false: true; 
}

//-------------------------------------------------------------------------------------------------
//			PROTECTED
//-------------------------------------------------------------------------------------------------

bool ProcParamDef::Validate()
{
	bool rc = false;

	if ( _index == IPluginICProcStepsParamDef::NotFound)
		ImageExptLib::LogMsg("ProcParamDef::Validate", "ERROR: _index == IPluginICProcStepsParamDef::NotFound");	
	else
	{	
		if (_Name == IPluginICProcStepsParamDefBadValue)
			ImageExptLib::LogMsg("ProcParamDef::Validate", "ERROR: _Name == IPluginICProcStepsParamDefBadValue");	
		else
		{		
			if (_Description == IPluginICProcStepsParamDefBadValue) 
				ImageExptLib::LogMsg("ProcParamDef::Validate", "ERROR: _Description == IPluginICProcStepsParamDefBadValue");
			else
			{
				_error = false;
				rc = true;
			}
		}
	}
	return rc;
}
//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------